<?php

/**
 * index.php
 *
 * Peopletab controller
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license
 * @filesource
 */
require_once('../Framework.php');

$result = Framework::init('Peopletab');

$cacheSettings =& Framework::$site->config('Framework', 'Cache');

if ((!isset($_COOKIE[session_name()]) ||
    !strlen($_COOKIE[session_name()]) ||
    $_COOKIE['cacheToggle'] == 1)
    && !count($_POST) && $cacheSettings['fullPageCaching']) {
	
    $doCache = true;
	$cache = Framework_Cache::factory('PageCache', 700);
	if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
	$_SERVER ['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest'){
		$ajaxFactor = 'true';
	} else {
		$ajaxFactor = 'false';
	}
	// Display from cache
	if ($cachedPage = $cache->get(Framework::$site->selfURL().$ajaxFactor)) {
		echo $cachedPage;
		return;
	}
} else {
	$doCache = false;
}

try {
	if ($doCache) {
		ob_start();
	   
	}
	$result = Framework::start('Peopletab');

	// If a PEAR error is returned usually something catastrophic happend like an
	// event returning a PEAR_Error or throwing an exception of some sort.
	if (PEAR::isError($result)) {
		Framework_Exception::display($result->getMessage());
		return;
	}
	
	
	// Cache teh page
	$user = Framework_User::singleton();
	// Full Page Caching
	if (in_array(Framework::$request->module, $cacheSettings['notToPageCache'])) {
		$cacheSettings['fullPageCaching'] = false;
	}

	if ($doCache) {
		$page = ob_get_contents();

		if ($cache && $cacheSettings['fullPageCaching'] && $user->isDefault()) {
			$cache->save($page,Framework::$site->selfURL().$ajaxFactor);
			if (!isset($_COOKIE['cacheToggle'])) {
				setcookie('cacheToggle', 1, time()+60*60*24*30, '/');
			}
		} else if(!$user->isDefault()) {
			setcookie('cacheToggle', 0, time()+60*60*24*30, '/');
		}
		ob_end_flush();
	}
} catch (Exception $e) {
	Framework_Exception::display($e->getMessage());
	return;
}



// Run shutdown functions and stop the Framework
Framework::stop();

?>